!preparing data for calculations
subroutine init3d(a,i,i_vertex2D,i_nVer2D,i_nInCat2D,i_vertex3D,i_nVer3D,&
    i_nInCat3D,i_igT,i_jgT,i_ggT,i_ig,i_jg,i_ggl,i_ggu,i_di,i_f,i_l1,i_v1,&
    i_v2,i_v3,i_v4,i_v5,i_ggl2,i_ggu2,i_di2,i_vnRZ,i_vnXYZ,i_igT3D,i_jgT3D,&
    i_ggT3D,i_l13d,i_va,i_ig3D,i_jg3D,i_igEarth,i_jgEarth,i_a,i_nodesforedges,&
    i_a0,i_params,vn2d_dat,vn3d_dat,va_dat,ax_dat,ay_dat,az_dat,a_0000,aXYZ)
    dimension a(120000000),i(100000000)
    real*8 a,eps2d,eps3d,eps3dNS,radiusRED
    integer inftry_dat,t_size_dat,tsize3d_dat,xyz_dat,rz_dat,tsize_dat,&
    gg_dat,gg3d_dat,vn2d_dat,vn3d_dat,ax_dat,ay_dat,az_dat,va_dat,edges_dat,&
    a_0000,sig3d,aXYZ
    integer elemsCount2D,edgesTermCount3D,edgesNotTermCount3D,edgesCount3D,&
    elemsCount3D,elementsEarth
    integer typeTask,typeTask_txt
    common /files_common/ infite_0,num_slae,log_txt,typeTask_txt
    common /dim2D/nodesCount2D,elemsCount2D,nodesBoundCount2D,nodesTermCount2D,m2D,&
    mTerm2d,nodesNotTermCount2D
    common /dim3D/nodesCount3D,elemsCount3D,nodesBoundCount3D,edgesTermCount3D,&
    edgesNotTermCount3D,edgesCount3D,nodesTermCount3D,m3D,mTerm3d,nodesNotTermCount3D
    common /commonInfo/ eps2d,eps3d,eps3dNS,radiusRED,maxiter,typeTask,nt,nMaterials
    common /commonIndex/ indexI,indexA
    common /dimEarth/nodesNotTermEarth,mEarth

    !opening files
    call open_files(inftry_dat,t_size_dat,tsize3d_dat,xyz_dat,nver_dat,nvkat_dat,&
    inf2tr_dat,nvkat2d_dat,rz_dat,nvtr_dat,tsize_dat,ig_dat,jg_dat,gg_dat,l1_dat,&
    ig3d_dat,jg3d_dat,gg3d_dat,vn2d_dat,vn3d_dat,ax_dat,ay_dat,az_dat,&
    l13d_dat,va_dat,nodesforedges_dat,edges_dat,a_0000,sig3d,aXYZ)

    !input 2D data
101 format(' ISLAU=',i8,' INDKU1=',i8,' INDFPO=',i8)
102 format('KUZLOV=',i8,'    KPAR=',i8,'    KT1=',i8,'    KT2=',i8,'    KT3=',i8)
103 format('KISRS1=',i8,' KISRS2=',i8,' KISRS3=',i8,'   KBRS=',i8)
104 format('KREB4 =',i8,' KTSIM =',i8)
    read(inf2tr_dat,101)k,k,k
    read(inf2tr_dat,102) nodesCount2D,elemsCount2D,nodesBoundCount2D,k1,k2
    read(inf2tr_dat,103) k,k,k,k
    read(inf2tr_dat,104) kreb4,k
    read(tsize_dat,*)nodesTermCount2D
    nodesNotTermCount2D = nodesCount2D-nodesTermCount2D
    i_vertex2D      = indexA
    i_ggT           = i_vertex2D + 2*nodesCount2D
    i_nVer2D        = indexI
    i_nInCat2D      = i_nVer2D+4*elemsCount2D
    i_l1            = i_nInCat2D+elemsCount2D
    i_igT           = i_l1+nodesBoundCount2D
    i_jgT           = i_igT+nodesTermCount2D+1
    call input2D(a(i_vertex2D),i(i_nVer2D),i(i_nInCat2D),i(i_igT),i(i_jgT),&
    a(i_ggT),i(i_l1),rz_dat,nvkat2D_dat,nvtr_dat,l1_dat,ig_dat,jg_dat,gg_dat)
    i_listbeg       = i_jgT+mTerm2d
    i_ig            = i_listbeg+nodesNotTermCount2D
    i_jg            = i_ig+nodesNotTermCount2D+1
    call portrait(i(i_ig),i(i_jg),nodesCount2D,elemsCount2D,i(i_nVer2D),&
    m2d,i(i_listbeg),4,4,i(i_igT),i(i_jgT),mTerm2d,nodesNotTermCount2D)

    !input 3D data
105 format(' ISLAU=',i8,' INDKU1=',i8,' INDFPO=',i8)
106 format('KUZLOV=',i8,'    KPAR=',i8,'    KT1=',i8)
    read(inftry_dat,105)k,k,k
    read(inftry_dat,106) nodesCount3D,elemsCount3D,nodesBoundCount3D
    read(t_size_dat,*)edgesTermCount3D
    read(t_size_dat,*)edgesNotTermCount3D
    read(t_size_dat,*)edgesCount3D
    read(tsize3d_dat,*)nodesTermCount3D
    nodesNotTermCount3D = nodesCount3D-nodesTermCount3D
    i_params        = i_ggT+mTerm2d
    call inputParams_task3d(a(i_params),sig3d,nMaterials)
    call rotate(a(i_params),nMaterials)
    i_vertex3D      = i_params+10*nMaterials
    i_ggT3D         = i_vertex3D + 3*nodesCount3D
    i_nVer3D        = i_jg+m2d
    i_nInCat3D      = i_nVer3D+14*elemsCount3D
    i_nodesforedges = i_nInCat3D+elemsCount3D
    i_l13d          = i_nodesforedges+3*edgesCount3D
    i_igT3D         = i_l13d+nodesBoundCount3D
    i_jgT3D         = i_igT3D+nodesTermCount3D+1
    call input3D(a(i_vertex3D),i(i_nVer3D),i(i_nInCat3D),i(i_igT3D),i(i_jgT3D),&
    a(i_ggT3D),i(i_l13d),i(i_nodesforedges),xyz_dat,nvkat_dat,nver_dat,l13d_dat,&
    ig3d_dat,jg3d_dat,gg3d_dat,nodesforedges_dat,edges_dat)
    i_listbeg       = i_jgT3D+mTerm3d
    i_ig3D          = i_listbeg+nodesNotTermCount3D
    i_jg3D          = i_ig3D+nodesNotTermCount3D+1
    call portrait(i(i_ig3D),i(i_jg3D),nodesCount3D,elemsCount3D,i(i_nVer3D),&
    m3d,i(i_listbeg),14,8,i(i_igT3D),i(i_jgT3D),mTerm3d,nodesNotTermCount3D)
    i_igEarth       = i_jg3D+m3d
    i_jgEarth       = i_igEarth+nodesNotTermEarth+1
    call getPortraitForEarth(i(i_ig3D),i(i_jg3D),i(i_igEarth),i(i_jgEarth))    
    
    !memory for solving SLAE and for solutions
    m = max(m2d,m3d)
    nodesNotTermCount = max(nodesNotTermCount2D,nodesNotTermCount3D)
    nodesCount = max(nodesCount2D,nodesCount3D)
    i_ggl           = i_ggT3D+mTerm3d
    i_ggu           = i_ggl+m
    i_di            = i_ggu+m
    i_f             = i_di+nodesNotTermCount
    i_v1            = i_f+nodesNotTermCount*3
    i_v2            = i_v1+nodesNotTermCount
    i_v3            = i_v2+nodesNotTermCount
    i_v4            = i_v3+nodesNotTermCount
    i_v5            = i_v4+nodesNotTermCount
    i_ggl2          = i_v5+nodesNotTermCount
    i_ggu2          = i_ggl2+m
    i_di2           = i_ggu2+m
    i_vnXYZ         = i_di2+nodesNotTermCount
    i_va            = i_vnXYZ+nodesCount
    i_vnRZ          = i_va
    i_a0            = i_vnXYZ
    i_a             = i_a0+edgesNotTermCount3D

    call close_files_task3d(inftry_dat,t_size_dat,tsize3d_dat,xyz_dat,&
    inver_dat,nvkat_dat,nf2tr_dat,nvkat2d_dat,rz_dat,nvtr_dat,tsize_dat,&
    ig_dat,jg_dat,gg_dat,l1_dat,ig3d_dat,jg3d_dat,gg3d_dat,&
    l13d_dat,nodesforedges_dat,edges_dat,sig3d)

1   format(A,i8,' nElement =',i8)
    write(*,1)'SP:    2d:            nVertex =',nodesCount2D,elemsCount2D
    write(*,1)'SP:    3d:            nVertex =',nodesCount3D,elemsCount3D
    write(log_txt,1)'SP:    2d:            nVertex =',nodesCount2D,elemsCount2D
    write(log_txt,1)'SP:    3d:            nVertex =',nodesCount3D,elemsCount3D
end
!*************************************************************************
!opening files
subroutine open_files(inftry_dat,t_size_dat,tsize3d_dat,xyz_dat,nver_dat,nvkat_dat,&
    inf2tr_dat,nvkat2d_dat,rz_dat,nvtr_dat,tsize_dat,ig_dat,jg_dat,gg_dat,l1_dat,&
    ig3d_dat,jg3d_dat,gg3d_dat,vn2d_dat,vn3d_dat,ax_dat,ay_dat,az_dat,&
    l13d_dat,va_dat,nodesforedges_dat,edges_dat,a_0000,sig3d,aXYZ)
    integer inftry_dat,t_size_dat,tsize3d_dat,xyz_dat,rz_dat,tsize_dat,&
    gg_dat,gg3d_dat,vn2d_dat,vn3d_dat,ax_dat,ay_dat,az_dat,va_dat,edges_dat,&
    a_0000,sig3d,aXYZ

   !mesh2D 
    inf2tr_dat  = 201
    nvkat2d_dat = 202
    rz_dat      = 203
    nvtr_dat    = 204
    tsize_dat   = 205
    ig_dat      = 206
    jg_dat      = 207
    gg_dat      = 208
    l1_dat      = 209
    open(inf2tr_dat,file='.\calculations\mesh2d\inf2tr.dat',mode='read')
    open(nvkat2d_dat,file='.\calculations\mesh2d\nvkat2d.dat',access='direct',recl=4,mode='read')
    open(rz_dat,file='.\calculations\mesh2d\rz.dat',access='direct',recl=16,mode='read')
    open(nvtr_dat,file='.\calculations\mesh2d\nvtr.dat',access='direct',recl=24,mode='read')
    open(tsize_dat,file='.\calculations\mesh2d\tsize.dat',mode='read')
    open(ig_dat,file='.\calculations\mesh2d\ig.dat',access='direct',recl=4,mode='read')
    open(jg_dat,file='.\calculations\mesh2d\jg.dat',access='direct',recl=4,mode='read')
    open(gg_dat,file='.\calculations\mesh2d\gg.dat',access='direct',recl=8,mode='read')
    open(l1_dat,file='.\calculations\mesh2d\l1.dat',access='direct',recl=4,mode='read')

    !mesh3D
    inftry_dat  = 101
    t_size_dat  = 102
    tsize3d_dat = 103
    xyz_dat     = 104
    nver_dat    = 105
    nvkat_dat   = 106
    ig3d_dat    = 107
    jg3d_dat    = 108
    gg3d_dat    = 109
    l13d_dat    = 110
    nodesforedges_dat = 111
    edges_dat   = 112
    sig3d       = 113
    open(inftry_dat,file='.\calculations\mesh3d\inftry.dat',mode='read')
    open(t_size_dat,file='.\calculations\mesh3d\t_size.dat',mode='read')
    open(tsize3d_dat,file='.\calculations\mesh3d\tsize3d.dat',mode='read')
    open(xyz_dat,file='.\calculations\mesh3d\xyz.dat',access='direct',recl=24,mode='read')
    open(nver_dat,file='.\calculations\mesh3d\nver.dat',access='direct',recl=56,mode='read')
    open(nvkat_dat,file='.\calculations\mesh3d\nvkat.dat',access='direct',recl=4,mode='read')
    open(ig3d_dat,file='.\calculations\mesh3d\ig3d.dat',access='direct',recl=4,mode='read')
    open(jg3d_dat,file='.\calculations\mesh3d\jg3d.dat',access='direct',recl=4,mode='read')
    open(gg3d_dat,file='.\calculations\mesh3d\gg3d.dat',access='direct',recl=8,mode='read')
    open(l13d_dat,file='.\calculations\mesh3d\l13d.dat',access='direct',recl=4,mode='read')
    open(nodesforedges_dat,file='.\calculations\mesh3dv\nodesforedges.dat',access='direct',recl=8,mode='read')
    open(edges_dat,file='.\calculations\mesh3dv\edges.dat',access='direct',recl=100,mode='read')
    open(sig3d,file='.\calculations\sig3d_anisotropic',mode='read')

    !solutions
    vn2d_dat = 401
    vn3d_dat = 402
    va_dat   = 403
    ax_dat   = 404
    ay_dat   = 405
    az_dat   = 406
    aXYZ     = 407
    a_0000   = 408
    open(vn2d_dat,file='.\calculations\vn2d.dat',access='direct',recl=8,status='replace',mode='write')
    open(vn3d_dat,file='.\calculations\vn3d.dat',access='direct',recl=8,status='replace',mode='write')
    open(va_dat,file='.\calculations\va.dat',access='direct',recl=8,status='replace',mode='write')
    open(ax_dat,file='.\calculations\ax.dat',access='direct',recl=8,status='replace',mode='write')
    open(ay_dat,file='.\calculations\ay.dat',access='direct',recl=8,status='replace',mode='write')
    open(az_dat,file='.\calculations\az.dat',access='direct',recl=8,status='replace',mode='write')
    open(aXYZ,file='.\calculations\aXYZ.dat',access='direct',recl=24,status='replace',mode='write')
    open(a_0000,file='.\calculations\A\a.0000',access='direct',recl=8,status='replace',mode='write')
end
!*************************************************************************
subroutine input2D(vertex,nVer,nInCat2D,igT,jgT,ggT,l1,rz_dat,nvkat2D_dat,&
    nvtr_dat,l1_dat,ig_dat,jg_dat,gg_dat)    
    common /dim2D/nodesCount2D,elemsCount2D,nodesBoundCount2D,nodesTermCount2D,&
    m2D,mTerm2d,nodesNotTermCount2D
    integer rz_dat,nvkat2D_dat,nvtr_dat,l1_dat,ig_dat,jg_dat,gg_dat
    integer elemsCount2D,edgesTermCount3D,edgesNotTermCount3D,edgesCount3D,elemsCount3D
    real*8 vertex,ggT,minR,maxR,minZ,maxZ
    dimension vertex(2,nodesCount2D),nVer(4,elemsCount2D),nInCat2D(elemsCount2D),&
    igT(nodesTermCount2D+1),jgT(*),ggT(*),l1(nodesBoundCount2D)

    call readMesh2D(rz_dat,nvkat2D_dat,nvtr_dat,l1_dat,vertex,nInCat2D,nVer,&
    l1,nodesCount2D,elemsCount2D,nodesBoundCount2D,minR,maxR,minZ,maxZ)    
    call readMatrixT(ig_dat,jg_dat,gg_dat,igT,jgT,ggT,nodesTermCount2D,mTerm2d) 
end
!*************************************************************************
subroutine input3D(vertex,nVer,nInCat3D,igT3D,jgT3D,ggT3D,l13d,nodesforedges,&
    xyz_dat,nvkat_dat,nver_dat,l13d_dat,ig3d_dat,jg3d_dat,gg3d_dat,nodesforedges_dat,&
    edges_dat)
    common /dim3D/nodesCount3D,elemsCount3D,nodesBoundCount3D,edgesTermCount3D,&
    edgesNotTermCount3D,edgesCount3D,nodesTermCount3D,m3D,mTerm3d,nodesNotTermCount3D
    common /dimEarth/nodesNotTermEarth,mEarth
    integer xyz_dat,nvkat_dat,nver_dat,l13d_dat,ig3d_dat,jg3d_dat,gg3d_dat,edges_dat
    integer edgesTermCount3D,edgesNotTermCount3D,edgesCount3D,elemsCount3D,edges
    real*8 vertex,ggT3D,minX,maxX,minY,maxY,minZ,maxZ
    dimension vertex(3,nodesCount3D),nVer(14,elemsCount3D),nInCat3D(elemsCount3D),&
    igT3D(nodesTermCount3D+1),jgT3D(*),ggT3D(*),l13d(nodesBoundCount3D),&
    nodesforedges(3,edgesCount3D)

    call readMesh3D(xyz_dat,nvkat_dat,nver_dat,l13d_dat,nodesforedges_dat,edges_dat,&
    vertex,nInCat3D,nVer,l13d,nodesforedges,nodesCount3D,elemsCount3D,edgesCount3D,&
    nodesBoundCount3D,minX,maxX,minY,maxY,minZ,maxZ)
    call readMatrixT(ig3d_dat,jg3d_dat,gg3d_dat,igT3D,jgT3D,ggT3D,nodesTermCount3D,mTerm3d)

    nodesNotTermEarth=1
    do j=1,nodesCount3D
        if(j.le.nodesNotTermCount3D.and.vertex(3,j).le.0.d0.and.nodesNotTermEarth.lt.j) nodesNotTermEarth=j
    enddo
end
!*************************************************************************
subroutine close_files_task3d(inftry_dat,t_size_dat,tsize3d_dat,xyz_dat,&
    inver_dat,nvkat_dat,nf2tr_dat,nvkat2d_dat,rz_dat,nvtr_dat,tsize_dat,&
    ig_dat,jg_dat,gg_dat,l1_dat,ig3d_dat,jg3d_dat,gg3d_dat,&
    l13d_dat,nodesforedges_dat,edges_dat,sig3d)
    integer inftry_dat,t_size_dat,tsize3d_dat,xyz_dat,rz_dat,tsize_dat,&
    gg_dat,gg3d_dat,edges_dat,sig3d
    close(inftry_dat)
    close(t_size_dat)
    close(tsize3d_dat)
    close(xyz_dat)
    close(nver_dat)
    close(nvkat_dat)
    close(inf2tr_dat)
    close(nvkat2d_dat)
    close(rz_dat)
    close(nvtr_dat)
    close(tsize_dat)
    close(ig_dat)
    close(jg_dat)
    close(gg_dat)
    close(l1_dat)
    close(ig3d_dat)
    close(jg3d_dat)
    close(gg3d_dat)
    close(l13d_dat)
    close(nodesforedges_dat)
    close(edges_dat)
    close(sig3d)
end
!*************************************************************************
!*************************************************************************
!*************************************************************************